# -- coding: utf-8 -- 
# Name: app

import os,time,platform,form
from flask import Flask,render_template,request,send_from_directory
from os import path

from werkzeug.datastructures import CombinedMultiDict
from werkzeug.utils import secure_filename

app = Flask(__name__)
if platform.system() == "Windows":
    slash = "\\"
else:
    platform.system() == "Linux"
    slash = "/"
UPLOAD_PATH = os.path.curdir + slash + 'uploads' + slash

@app.route('/',methods=['GET','POST'])
def hello_world():
    if request.method == 'GET':
        return render_template('upload.html')
    else:
        if not os.path.exists(UPLOAD_PATH):
            os.makedirs(UPLOAD_PATH)
            form = UploadForm(CombinedMultiDict([request.form, request.files]))
            if form.validate():
                f = request.files['file']
                filename = secure_filename(f.filename)
                ext = filename.rsplit('.',1)[1]
                unix_time = int(time.time())
                new_filename = str(unix_time) + '.' + ext
                file_url = UPLOAD_PATH + new_filename
                f.save(path.join(UPLOAD_PATH,new_filename))
                return "upload success"
            else:
                return "only can upload jpg,png,gif file"

@app.route("/images/<filename>/",methods=['GET','POST'])
def get_image(filename):
    dirpath = os.path.join(app.root_path,'uploads')
    return send_from_directory(dirpath,filename)

if __name__ == '__main__':
    app.run(debug=True)
